package com.dexstudio.threads;

import java.io.IOException;
import java.util.LinkedList;

import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

import com.dexstudio.R;
import com.dexstudio.currency.entity.Currency;
import com.dexstudio.exceptions.EmptyXMLException;
import com.dexstudio.http.HttpData;
import com.dexstudio.http.HttpRequest;
import com.dexstudio.http.constants.CurrencyRequestTypes;
import com.dexstudio.http.constants.P24Urls;
import com.dexstudio.xmlparser.CurrencyParser;

import android.content.res.Resources;
import android.os.AsyncTask;
import android.util.Log;

public class Task extends AsyncTask<Void, String, Boolean> {

	protected final Resources mResources;
    
    private Boolean mResult;
    private String mProgressMessage;
    private IProgressTracker mProgressTracker;	
    private LinkedList<Currency> currencyList = null; 
	
    /* UI Thread */
    public Task(Resources resources) {
    	// Keep reference to resources
    	mResources = resources;
    	// Initialize initial preexecute message
    	mProgressMessage = resources.getString(R.string.taskWorking);
    }
    
    /* UI Thread */
    public void setProgressTracker(IProgressTracker progressTracker) {
		// Attach to progress tracker
		mProgressTracker = progressTracker;
		// Initialize progress tracker with current task state
		if (mProgressTracker != null) {
		    mProgressTracker.onProgress(mProgressMessage);
		    if (mResult != null) {
		    	mProgressTracker.onComplete();
		    }
		}
    }
    
    /* UI Thread */
    @Override
    protected void onCancelled() {
		// Detach from progress tracker
		mProgressTracker = null;
    }
    
    /* UI Thread */
    @Override
    protected void onProgressUpdate(String... values) {
		// Update progress message 
		mProgressMessage = values[0];
		// And send it to progress tracker
		if (mProgressTracker != null) {
		    mProgressTracker.onProgress(mProgressMessage);
		}
    }
    
    /* UI Thread */
    @Override
    protected void onPostExecute(Boolean result) {
		// Update result
		mResult = result;
		// And send it to progress tracker
		if (mProgressTracker != null) {
		    mProgressTracker.onComplete();
		}
		// Detach from progress tracker
		mProgressTracker = null;
    }
    
	@Override
	protected Boolean doInBackground(Void... params) {
		//Check if task is cancelled
		if (isCancelled()) {
			// This return causes onPostExecute call on UI thread
			return false;
		}
		try {
			publishProgress(mResources.getString(R.string.taskWorking));
		    	
			String sUrl = P24Urls.prepareCurrencyLink(CurrencyRequestTypes.NBU_COURSES);
				
			HttpData httpData = HttpRequest.get(sUrl);
			CurrencyParser currencyParser = new CurrencyParser(httpData.content);
			setCurrencyList(currencyParser.parse());
		} catch (ParserConfigurationException e) {
			Log.e("XMLParse", "Error: " + e.getMessage());
			//e.printStackTrace();
			return false;
		} catch (SAXException e) {
			Log.e("XMLParse", "Error: " + e.getMessage());
			return false;
		} catch (IOException e) {
			Log.e("XMLParse", "Error: " + e.getMessage());
			return false;
		} catch (EmptyXMLException e) {
			Log.e("XMLParse", "Error: " + e.getMessage());
			return false;
		} catch (Exception e) {
			Log.e("XMLParse", "Error: " + e.getMessage());
			return false;
		}
		// This return causes onPostExecute call on UI thread
		return true;
	}

	private void setCurrencyList(LinkedList<Currency> currencyList) {
		this.currencyList = currencyList;
	}

	public LinkedList<Currency> getCurrencyList() {
		return currencyList;
	}
}
